﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DatabaseSynchronizer {
    public class Settings {
        public string ConnectionString { get; set; }

        public string DisplayConnectionString {

            get {
                if (!string.IsNullOrEmpty(ConnectionString)
                    && ConnectionString.ToLower().Contains("password")) {

                    try {

                        string passwordSection = ConnectionString.Split(';').First(s => s.TrimStart().ToLower().StartsWith("password"));
                        string password = passwordSection.Split('=').Last();
                        string hiddenPassword = string.Empty;
                        password.ToCharArray().ToList().ForEach(c => hiddenPassword += "*");

                        return ConnectionString.Replace(passwordSection, passwordSection.Replace(password, hiddenPassword));
                    }
                    catch {
                        return ConnectionString;
                    }
                }
                else {
                    return ConnectionString;
                }
            }
        }

        public bool IsTFS { get; set; }

        public string LocalScriptsFolder { get; set; }

        public string LoggedInUser { get; set; }
        public string MachineName { get; set; }
        public string TFSServerURL { get; set; }
        public string TFSScriptsFolder { get; set; }

        public string TFSId { get; set; }
        public string TFSPwd { get; set; }

        public Database Database { get; set; }
    }


    public static class SettingsHelpers {

        public static string DataSource(this Settings settings) {

            if (settings != null && !string.IsNullOrEmpty(settings.ConnectionString)
                && settings.Database == Database.SQL) {

                var connStrList = settings.ConnectionString.Split(';').ToList();

                try {
                    return connStrList.Single(c => c.ToLower().StartsWith("data source")).Split('=').Last();
                }
                catch {
                    return string.Empty;
                }
            }
            else {

                return string.Empty;
            }
        }

        public static string InitialCatalog(this Settings settings) {

            if (settings != null && !string.IsNullOrEmpty(settings.ConnectionString)
                && settings.Database == Database.SQL) {

                var connStrList = settings.ConnectionString.Split(';').ToList();

                try {
                    return connStrList.Single(c => c.ToLower().StartsWith("initial catalog")).Split('=').Last();
                }
                catch {
                    return string.Empty;
                }
            }
            else {

                return string.Empty;
            }
        }

    }
}
